﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MS.Core.Model;
using ReportDesignerServer.Services;
using ReportDesignerServer.Vo;
using MS.Core.Helper;

namespace ReportDesignerServer.Controllers
{


    /// <summary>
    /// 护士工作量
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthController : ControllerBase
    {       
        private TokenHelper _tokenHelper;
        private UserService userService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tokenHelper"></param>
        /// <param name="userService"></param>
        public AuthController( TokenHelper tokenHelper, UserService userService)
        { 
            _tokenHelper = tokenHelper;
            this.userService = userService;
        } 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt Login([FromBody] LoginParam param)
        {
            var getUser = userService.GetLoginUser(param.username, param.password);
            if (getUser != null)
            {
                var token = _tokenHelper.CreateToken(getUser.id.ToString(), getUser.nickname);
                return Rt.SuccessObj(token);
            }
            return Rt.Fail("登录失败");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt PLogin([FromBody] LoginParam param)
        {
            var getUser = userService.GetLoginUser(param.username, param.password);
            if (getUser != null)
            {
                return Rt.SuccessObj(getUser);
            }
            return Rt.Fail(null);
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpDelete]
        public Rt Logout()
        {
            return Rt.Success( );
        }        

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Result(string token, string refresh_token)
        {
            return Ok(_tokenHelper.Refresh(token, refresh_token));
        }
    }
}